package trong.gold.soft.com.view;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import trong.gold.soft.com.main.mTetrisMidlet;
import trong.gold.soft.com.model.Diem;
import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.font.VietnameseFont;

/**
 *
 * @author trong
 */
public class loadingTetris extends GameCanvas {

    public Graphics g;
    public Image img;
    public Diem point = new Diem(30, 50);
    public Font fontNormal;
    public Font fontBold;

    public int colorBorder = 0x000000;
    public int colorLoading = 0xcc3301;
    
    public int loadingLenght = 100;
    public int loadingWeight = 5;
    public int loadCurrent = 0;
    public int h = 10;
    
    private String imgName="logo.png";
    private mTetrisMidlet midlet;
    public loadingTetris(mTetrisMidlet midlet) {
        super(false);
        this.midlet = midlet;
        g = this.getGraphics();
        fontNormal = g.getFont();
        fontBold = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        setFullScreenMode(true);
        img = createImg.createImage("/com/image/load/" + imgName);
        point = new Diem(getWidth() / 2 - img.getWidth() / 2, point.getY());
    }
    public void start() {
        clearScreen();
        draw();
        Draw_Next_Loading();
    }

    public void draw() {
    	g.setColor(0x000000);
    	g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(img, point.getX(), point.getY(), Graphics.TOP | Graphics.LEFT);
        g.setFont(fontBold);
        //g.drawString(VietnameseFont.Black.DrawString(g, "Đang tải", 0, 0, Graphics.HCENTER), getWidth() / 2 - fontBold.stringWidth(text) / 2, root.getY() + img.getHeight() + 40, Graphics.TOP | Graphics.LEFT);
        VietnameseFont.White.DrawString(g, "Đang tải", getWidth() / 2, point.getY() + img.getHeight() + 40, Graphics.HCENTER);
        g.setFont(fontNormal);
        
        g.setColor(colorBorder);
        g.drawRect(getWidth() / 2 - loadingLenght / 2, point.getY() + img.getHeight() + 30, loadingLenght, loadingWeight);
        g.setColor(colorLoading);
        g.fillRect(getWidth() / 2 - loadingLenght / 2, point.getY() + img.getHeight() + 30, loadCurrent, loadingWeight);
        g.setColor(colorBorder);
        
        this.flushGraphics();
    }

    public void Draw_Next_Loading() {
        clearLoading();
        for (int i = 0; i < loadingLenght / h; i++) {
            if (loadCurrent < loadingLenght) {
                loadCurrent += h;
                g.setColor(colorBorder);
                g.drawRect(getWidth() / 2 - loadingLenght / 2, point.getY() + getHeight() + 30, loadingLenght, loadingWeight);
                g.setColor(colorLoading);
                g.fillRect(getWidth() / 2 - loadingLenght / 2, point.getY() + img.getHeight() + 30, loadCurrent, loadingWeight);
                g.setColor(colorBorder);
                this.flushGraphics();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            } else {
                loadCurrent = 0;
                System.out.println("abc abc");
                break;
            }
        }
        setNull();
        Runtime.getRuntime().gc();
        midlet.switchState(this, new slSound(midlet));
       
    }

    private void clearScreen() {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        this.flushGraphics();
    }

    private void clearLoading() {
        g.setColor(0xffffff);
        g.fillRect(getWidth() / 2 - loadingLenght / 2, point.getY() + img.getHeight() + 30, loadingLenght, loadingWeight);
        this.flushGraphics();

    }

    private void setNull() {
        g = null;
        img=null;
        point = null;
        fontNormal = null;
        fontBold = null;
        imgName=null;
        
    }
}
